package com.ynrcc.util;

import java.util.List;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

public class ImportMain {
	public static boolean importMain(String basedir, String sourceId, String brchcd) {
		String content = "";
		String path = basedir + sourceId + "\\main.txt";
		content = ReadFile.readFile(path);

		// 判断记录是否已经存在s
		String sql = "select * from doc where source_id = '" + sourceId + "'";
		List<Record> docs = Db.find(sql);
		if (docs.size() > 0) {
			System.out.println("记录已存在，导入失败");
			return false;
		}

		if (content.trim().length() == 0) {
			System.out.println("文件读取失败");
			return false;
		} else {
			System.out.println("文件读取成功，文件长度[" + content.trim().length() + "]");
		}

		// 中文名称替换为字段名称
		content = content.replaceFirst("状态", "STATUS");
		content = content.replaceFirst("标题", "TITLE");
		content = content.replaceFirst("录入者", "INPUTER");
		content = content.replaceFirst("录入时间", "INPUT_TIME");
		content = content.replaceFirst("盒号", "BOX_ID");
		content = content.replaceFirst("公文类别", "DOC_CATEGORY");
		content = content.replaceFirst("类别", "FILE_CATEGORY");
		content = content.replaceFirst("分类号", "SUB_CATEGORY_ID");
		content = content.replaceFirst("件号", "PACK_ID");
		content = content.replaceFirst("档号", "FILE_NUMBER");
		content = content.replaceFirst("单位名称", "ORG_NAME");
		content = content.replaceFirst("单位代码", "ORG_CODE");
		content = content.replaceFirst("归档机构", "INPUT_ORG");
		content = content.replaceFirst("保管期限", "TIME_LIMIT");
		content = content.replaceFirst("归档年度", "FILE_YEAR");
		content = content.replaceFirst("全宗号", "ARCHIVE_ID");
		content = content.replaceFirst("机构问题", "BRCH_NAME");
		content = content.replaceFirst("机构问题代码", "BRCH_CODE");
		content = content.replaceFirst("文件页数", "PAGES");
		content = content.replaceFirst("形成时间", "GENERATED_DATE");
		content = content.replaceFirst("发文编号", "SEND_DOC_NUMBER");
		content = content.replaceFirst("收文编号", "RECEIVE_DOC_NUMBER");
		content = content.replaceFirst("来文文号", "FROM_DOC_NUMBER");
		content = content.replaceFirst("责任者", "AUTHOR");
		content = content.replaceFirst("主题词", "SUBJECT");
		content = content.replaceFirst("密级", "SECURITY_LEVEL");
		content = content.replaceFirst("载体规格", "CARRIER_SIZE");
		content = content.replaceFirst("载体类型", "CARRIER_TYPE");
		content = content.replaceFirst("稿本", "SCRIPT");
		content = content.replaceFirst("文种", "DOC_TYPE");
		content = content.replaceFirst("缩微号", "MICRO_ID");
		content = content.replaceFirst("提要", "ABSTRACT");
		content = content.replaceFirst("开放级别", "OPEN_LEVEL");
		content = content.replaceFirst("纸质文档号", "PAPER_ID");
		content = content.replaceFirst("电子文档号", "ELECTRONIC_ID");
		content = content.replaceFirst("原文件名", "ORIGINAL_NAME");
		content = content.replaceFirst("储存位置", "STORAGE_PLACE");
		content = content.replaceFirst("附注", "COMMENTS");
		content = content.replaceFirst("阅读者", "READER");

		System.out.println(content);
		// 数据库操作
		// 取主键
		sql = "select SEQ_DOC.nextval SEQ from dual";
		int seq = Db.findFirst(sql).getInt("SEQ");
		Record doc = new Record().set("ID", seq).set("BRCHCD", brchcd).set("SOURCE_ID", sourceId);

		String[] column = content.split("●");
		for (String pair : column) {
			int pos = pair.lastIndexOf("■");
			String name = pair.substring(0, pos);
			String value = pair.substring(pos + 1);
			// System.out.println(name + "■" + value );
			doc.set(name, value);
		}
		Db.save("doc", doc);

		return true;
	}
}
